package com.yang.jpa.controller;

import com.alibaba.fastjson.JSONObject;
import com.yang.jpa.model.Member;
import com.yang.jpa.model.Player;
import com.yang.jpa.model.PutPlayer;
import com.yang.jpa.service.MemberServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class MemberController {
    @Autowired
    private MemberServiceImp memberServiceImp;

    @PostMapping("/queryAll")
    public List<Member> queryAll(){
        return memberServiceImp.queryAll();
    }

    @PostMapping("/insertAll")
    public Map<String, Object> insertAll(@RequestParam(name = "name") String name, @RequestParam(name = "age") String age){
        List<Member> mList = JSONObject.parseArray(age, Member.class);
        System.out.println(mList);
        return memberServiceImp.insertAll(mList);
    }

    @GetMapping("/jpa")
    public Map<String, Object> jpaTest(){
        return memberServiceImp.jpaTest();
    }

    @PostMapping("/dynamicQuery")
    public List<Player> dynamicQuery(@RequestParam(name = "player") String playerStr,
                                            @RequestParam(name = "page") String page,
                                            @RequestParam(name = "size") String size){

        PutPlayer player = JSONObject.parseObject(playerStr, PutPlayer.class);
        return memberServiceImp.dynamicQuery(player, Integer.parseInt(page), Integer.parseInt(size));
    }
}
